<template>
  <div>
    <div>
      <el-form :inline="true"
               size="mini"
               style="height: 40px">
        <el-form-item>
          <report-select-way v-model="selectTime"></report-select-way>
        </el-form-item>

        <el-form-item>
          <el-input size="mini"
                    @keyup.enter.native="handleIconClick"
                    v-model="pagination.params.searchValue"
                    placeholder="座席姓名.工号"
                    clearable>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="handleNodeClick">查询</el-button>
        </el-form-item>

        <el-form-item>
          <el-button @click="exportToExcel">导出excel</el-button>
        </el-form-item>
      </el-form>

    </div>

    <dd-table height="calc(100vh - 200px)"
              :data="tableData"
              :config="config"
              border
              :loading="loading"
              @filter-change="filterHandler"
              :current-page="pagination.currentPage"
              :total="pagination.total"
              @current-change="changePage"
              :page-size="pagination.pageSize"
              @size-change="handleSizeChange">
    </dd-table>
  </div>
</template>
<script>
import commonApi from '@/api/common.js'
import { Notification } from 'element-ui'
// 电话坐席工作情况表
export default {
  data () {
    return {
      filterList: [],
      tableData: [],
      loading: false,
      config: [
        {
          label: '序号',
          prop: 'indexnum',
          width: '60'
        },
        {
          label: '日期',
          prop: 'ldate',
          'show-overflow-tooltip': true
        },
        {
          label: '组别',
          prop: 'workGroup',
          'show-overflow-tooltip': true,
          'column-key': 'group',
          filters: []
        },
        {
          label: '座席姓名',
          prop: 'agentName',
          'show-overflow-tooltip': true
        },
        {
          label: '工号',
          prop: 'agentId',
          'show-overflow-tooltip': true
        },
        {
          label: '人工接通量',
          prop: 'humanAnsNum',
          'show-overflow-tooltip': true
        },
        {
          label: '平均通话时长',
          prop: 'avgCallDur',
          'show-overflow-tooltip': true
        },
        {
          label: '平均后处理时长',
          prop: 'avgPostProcDur',
          'show-overflow-tooltip': true
        },
        {
          label: '平均处理时长',
          prop: 'avgProcDur',
          'show-overflow-tooltip': true
        },
        {
          label: '值机比',
          prop: 'checkinRate',
          'show-overflow-tooltip': true
        },
        {
          label: '总外拨数量',
          prop: 'outcallNum',
          'show-overflow-tooltip': true
        },
        {
          label: '总外拨成功数量',
          prop: 'outcallSuccNum',
          'show-overflow-tooltip': true
        },
        {
          label: '满意度',
          prop: 'satisDegree',
          'show-overflow-tooltip': true
        },
        {
          label: '非常满意',
          prop: 'verySatis',
          'show-overflow-tooltip': true
        },
        {
          label: '满意',
          prop: 'satis',
          'show-overflow-tooltip': true
        },
        {
          label: '一般',
          prop: 'common',
          'show-overflow-tooltip': true
        },
        {
          label: '业务不满意',
          prop: 'busiDissatis',
          'show-overflow-tooltip': true
        },
        {
          label: '服务不满意',
          prop: 'servDissatis',
          'show-overflow-tooltip': true
        },
        {
          label: '质检成绩',
          prop: 'evalScore',
          'show-overflow-tooltip': true
        },
        {
          label: '普通工单差错量',
          prop: 'normalOrderErrs',
          'show-overflow-tooltip': true
        },
        {
          label: '重大工单差错量',
          prop: 'severeOrderErrs',
          'show-overflow-tooltip': true
        }
      ],
      selectTime: ['', ''],
      pagination: {
        currentPage: 1,
        total: 0,
        pageSize: 20,
        // 0 不导出 1导出
        isExport: '0',
        params: {
          reportType: '',
          selectTime: ['', ''],
          group: [],
          searchValue: ''
        }
      }
    }
  },
  mounted () {
    this.pagination.params.selectTime = ['', '']
    this.pagination.params.reportType = 'day'
    this.getTableData()
  },
  computed: {
    getHeight () {
      return this.$store.state.common.tableHeight
    }
  },
  methods: {
    // 导出excel表
    exportToExcel () {
      this.pagination.isExport = '1'
      let tHeader = []
      let filterVal = []
      for (let item of this.config) {
        filterVal.push(item.prop)
        tHeader.push(item.label)
      }
      commonApi.getPhoneWorkStatusTable(this.pagination).then(response => {
        if (response.retcode === '0') {
          require.ensure([], () => {
            const { export_json_to_excel } = require('@/utils/vendor/Export2Excel.js')
            const list = response.data.info
            const data = this.formatJson(filterVal, list)
            export_json_to_excel(tHeader, data, '电话坐席工作情况表')
          })
        } else {
          Notification.error('数据返回失败')
        }
      })
    },
    formatJson (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    filterHandler (val) {
      this.pagination.params.group = val['group']
      this.pagination.isExport = '0'
      this.getTableData()
    },
    handleNodeClick () {
      this.pagination.params.selectTime = this.selectTime.selectTime
      this.pagination.params.reportType = this.selectTime.reportType
      this.getTableData()
    },
    // 切换页码
    changePage (val) {
      this.pagination.currentPage = val
      this.getTableData()
    },
    // 可以调整每页条数
    handleSizeChange (val) {
      this.pagination.pageSize = val
      this.getTableData()
    },
    // 获取数据表
    getTableData () {
      this.pagination.isExport = '0'
      this.loading = true
      commonApi.getPhoneWorkStatusTable(this.pagination).then(response => {
        this.loading = false
        if (response.retcode === '0') {
          this.tableData = response.data.info
          this.pagination.total = response.data.total
          this.config[2]['filters'] = response.data.filterList
        } else {
          Notification.error('数据返回失败')
        }
      })
    }
  }
}
</script>
